12 August 2022

REPLICATION FILES FOR:  
	
	Measuring Swing Voters
	Christopher Hare and Mikayla Kutsuris
	Forthcoming in Political Analysis



INSTALL PACKAGES
================

For exact reproducibility, use R version 4.1.1.

Install the following packages from CRAN:

	install.packages(c("extraTrees","plyr","dplyr","MLmetrics",
	"doParallel","foreach","MASS","ggplot2","scales","MCMCpack",
	"BMS","gbm","ranger","kernlab","nnet","mmpf","psych",
	"reshape2","smacof","statar","viridis","poLCA","sparsereg",
	"compare","irr","Hmisc","gridExtra","prodlim","latticeExtra",
	"earth","mda","quadprog","RANN","vip","pdp","ROSE","mgcv",
	"tidyquant","stargazer","iml","forcats","rFSA","sjPlot",
	"rgenoud","minerva","fpc","rpart.plot","xtable","plotROC",
	"FindIt","evtree","pre","future","future.callr","stringr",
	"haven","coxed","separationplot","mltools","rfUtilities",
	"tidyverse","remotes","conquer","rms","doSNOW","progress",
	"bigparallelr","systemfonts"), dependencies=TRUE)

Alongside the following packages that are not presently available on CRAN:

	remotes:::install_local("./software/DMwR_0.4.1.tar.gz")
	remotes:::install_local("./software/emIRT_0.0.11.tar.gz")

For exact reproducibility, install the following versions of these packages:

	remotes:::install_local("./software/arm_1.11-2.tar.gz")
	remotes:::install_local("./software/caret_6.0-88.tar.gz")
	remotes:::install_local("./software/class_7.3-19.tar.gz")
	remotes:::install_local("./software/data.table_1.14.0.tar.gz")
	remotes:::install_local("./software/glmnet_4.1-2.tar.gz")
	remotes:::install_local("./software/Matrix_1.3-4.tar.gz")
	remotes:::install_local("./software/mvtnorm_1.1-2.tar.gz")
	remotes:::install_local("./software/nnet_7.3-16.tar.gz")
	remotes:::install_local("./software/party_1.3-8.tar.gz")
	remotes:::install_local("./software/partykit_1.2-13.tar.gz")
	remotes:::install_local("./software/permimp_1.0-1.tar.gz")
	remotes:::install_local("./software/pROC_1.17.0.1.tar.gz")
	remotes:::install_local("./software/recipes_0.1.16.tar.gz")
	remotes:::install_local("./software/rpart_4.1-15.tar.gz")
	remotes:::install_local("./software/survival_3.2-11.tar.gz")
	remotes:::install_local("./software/TTR_0.24.2.tar.gz")
	remotes:::install_local("./software/unbalanced_2.0.tar.gz")
	remotes:::install_local("./software/xgboost_1.4.1.1.tar.gz")
	remotes:::install_local("./software/yardstick_0.0.8.tar.gz")



INSTRUCTIONS
============

To replicate the paper's results, run the script "main.R". This file calls the following nine modules, with approximate run time (based on twelve-core processing using Intel Core i7-8700K CPU @ 3.70GHz, 16GB RAM) in brackets:

"./0_definefunctions.R" [< 1 minute]
"./1_datapreprocessing.R" [10 minutes]
"./2_ensemble.R" [22 hours; the "mainresult" ensemble runs in 3.5 hours]
"./3_sensitivityanalysis.R" [35 minutes]
"./4_modelassessment.R" [< 1 minute]
"./5_featurepermutations.R" [28 hours, using 500 trials]
"./6_featureablation.R" [17 hours]
"./7_imputeddataanalysis.R" [33 hours, using 500 trials]
"./8_featureinteractions.R" [35 hours]

Note: if memory problems arise, can close R between modules. They are designed to run independently (though sequentially).

The "Routput.txt" file includes complete output from a test run of "main.R". 



CORRESPONDENCE
==============

The figure file names correspond to those in the manuscript and appendix (e.g., "figure_1.pdf").

The tables can be replicated using the following objects/commands:

Table 2: "fitstats.table2" ("./2_ensemble.R"; line 167);
"fit.statistics(preds=GAM.preds, true.outcome=outsample$swingvoter)" ("./2_ensemble.R"; line 227)
Table 3: "leaveout.stats" ("./6_featureablation.R", line 603)
Table A.2: "summary(mainresult$res.probit)" ("./2_ensemble.R", line 111)
Table A.3: "cor(df.validation.comparison, use="pair")" ("./2_ensemble.R", line 149)
Table A.4: "externalindicator.futureswingvoter.df" ("./4_modelassessment.R", line 331)
Table A.5: "xtable::xtable(interaction.pairs.df[1:20,], auto=TRUE, digits=2)" ("./8_featureinteractions.R", line 131); 
"xtable::xtable(interaction.pairs.dfD[1:5,], auto=TRUE, digits=2)" ("./8_featureinteractions.R", line 171); 
"xtable::xtable(interaction.pairs.dfR[1:5,], auto=TRUE, digits=2)" ("./8_featureinteractions.R", line 174); 
"xtable::xtable(interaction.pairs.dfI[1:5,], auto=TRUE, digits=2)" ("./8_featureinteractions.R", line 177)
Table A.6: Written to the files "./results/predruleensemble_coefs_all_3L.csv" ("./8_featureinteractions.R", line 337); 
"./results/predruleensemble_coefs_D_3L.csv" ("./8_featureinteractions.R", line 339);  
"./results/predruleensemble_coefs_R_3L.csv" ("./8_featureinteractions.R", line 341); 
 "./results/predruleensemble_coefs_I_3L.csv ("./8_featureinteractions.R", line 343)
Table A.7: "xtable::xtable(state.wtd.means, auto=TRUE, digits=2)" ("./4_modelassessment.R", line 583)
Table A.8: "xtable::xtable(knowledge.wtd.means, auto=TRUE, digits=2)" ("./4_modelassessment.R", line 594)
Table A.9: "xtable::xtable(ideological.inconsistency.wtd.means, auto=TRUE, digits=2)" ("./4_modelassessment.R", line 605)
Table A.10: "xtable::xtable(knowledge.wtd.means.flip, auto=TRUE, digits=2)" ("./4_modelassessment.R", line 616)
Table A.11: "xtable::xtable(ideological.inconsistency.wtd.means.flip, auto=TRUE, digits=2)" ("./4_modelassessment.R", line 627)



SESSION INFO
============

R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
 [1] stats4    grid      parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
  [1] rfUtilities_2.1-5          mltools_0.3.5              coxed_0.3.3                rms_6.2-0                 
  [5] SparseM_1.81               haven_2.4.3                pre_1.0.1                  evtree_1.0-8              
  [9] partykit_1.2-13            libcoin_1.0-8              FindIt_1.2.0               plotROC_2.2.1             
 [13] xtable_1.8-4               rpart.plot_3.1.0           fpc_2.2-9                  minerva_1.5.10            
 [17] permimp_1.0-1              arm_1.11-2                 lme4_1.1-27.1              rgenoud_5.8-3.0           
 [21] sjPlot_2.8.10              rFSA_0.9.6                 iml_0.10.1                 stargazer_5.2.2           
 [25] tidyquant_1.0.3            quantmod_0.4.18            TTR_0.24.2                 PerformanceAnalytics_2.0.4
 [29] xts_0.12.1                 lubridate_1.7.10           yardstick_0.0.8            glmnet_4.1-2              
 [33] Matrix_1.3-4               mgcv_1.8-38                nlme_3.1-152               ROSE_0.0-4                
 [37] DMwR_0.4.1                 pdp_0.7.0                  vip_0.3.2                  RANN_2.6.1                
 [41] quadprog_1.5-8             mda_0.5-2                  class_7.3-19               earth_5.3.1               
 [45] plotmo_3.6.1               TeachingDemos_2.12         latticeExtra_0.6-29        data.table_1.14.0         
 [49] prodlim_2019.11.13         gridExtra_2.3              pROC_1.17.0.1              Hmisc_4.6-0               
 [53] Formula_1.2-4              survival_3.2-11            irr_0.84.1                 lpSolve_5.6.15            
 [57] compare_0.2-6              sparsereg_1.2              poLCA_1.4.1                scatterplot3d_0.3-41      
 [61] viridis_0.6.2              viridisLite_0.4.0          emIRT_0.0.11               Rcpp_1.0.8                
 [65] pscl_1.5.5                 statar_0.7.3               smacof_2.1-3               e1071_1.7-8               
 [69] colorspace_2.0-2           plotrix_3.8-2              reshape2_1.4.4             psych_2.1.9               
 [73] mmpf_0.0.5                 rpart_4.1-15               party_1.3-8                strucchange_1.5-2         
 [77] sandwich_3.0-1             zoo_1.8-9                  modeltools_0.2-23          mvtnorm_1.1-2             
 [81] glinternet_1.0.12          nnet_7.3-16                kernlab_0.9-29             ranger_0.13.1             
 [85] xgboost_1.4.1.1            gbm_2.1.8                  BMS_0.3.4                  MCMCpack_1.6-0            
 [89] coda_0.19-4                scales_1.1.1               systemfonts_1.0.2          forcats_0.5.1             
 [93] stringr_1.4.0              purrr_0.3.4                readr_2.0.1                tidyr_1.1.3               
 [97] tibble_3.1.4               tidyverse_1.3.1            MASS_7.3-55                doParallel_1.0.16         
[101] iterators_1.0.13           foreach_1.5.1              MLmetrics_1.1.1            recipes_0.1.16            
[105] dplyr_1.0.7                plyr_1.8.6                 caret_6.0-88               ggplot2_3.3.5             
[109] lattice_0.20-44           

loaded via a namespace (and not attached):
  [1] lmtest_0.9-38        crayon_1.4.1         backports_1.2.1      reprex_2.0.1         ellipse_0.4.2       
  [6] rlang_0.4.11         ROCR_1.0-11          readxl_1.3.1         performance_0.7.3    nloptr_1.2.2.2      
 [11] glue_1.4.2           mcmc_0.9-7           VGAM_1.1-5           tidyselect_1.1.1     sjmisc_2.8.7        
 [16] nnls_1.4             lars_1.2             MatrixModels_0.5-0   magrittr_2.0.1       cli_3.1.0           
 [21] rstudioapi_0.13      wordcloud_2.6        sjlabelled_1.1.8     polynom_1.4-0        xfun_0.26           
 [26] parameters_0.14.0    weights_1.0.4        cluster_2.1.2        expm_0.999-6         quantreg_5.86       
 [31] listenv_0.8.0        png_0.1-7            future_1.23.0        ipred_0.9-12         withr_2.4.2         
 [36] cellranger_1.1.0     pillar_1.6.2         RcppParallel_5.1.4   multcomp_1.4-17      fs_1.5.0            
 [41] flexmix_2.3-17       hash_2.2.6.1         vctrs_0.3.8          ellipsis_0.3.2       generics_0.1.0      
 [46] lava_1.6.10          tools_4.1.1          foreign_0.8-81       munsell_0.5.0        emmeans_1.6.3       
 [51] proxy_0.4-26         fastmap_1.1.0        compiler_4.1.1       abind_1.4-5          msm_1.6.8           
 [56] utf8_1.2.2           Quandl_2.11.0        jsonlite_1.7.2       carData_3.0-4        estimability_1.3    
 [61] lazyeval_0.2.2       car_3.0-12           effectsize_0.4.5     checkmate_2.0.0      igraph_1.2.6        
 [66] prabclus_2.3-2       htmltools_0.5.2      candisc_0.8-5        digest_0.6.27        assertthat_0.2.1    
 [71] bayestestR_0.11.0    future.apply_1.8.1   splines_4.1.1        broom_0.7.9          hms_1.1.0           
 [76] modelr_0.1.8         base64enc_0.1-3      mnormt_2.0.2         shape_1.4.6          tmvnsim_1.0-2       
 [81] inum_1.0-4           Metrics_0.1.4        coin_1.4-1           mclust_5.4.7         fansi_0.5.0         
 [86] conquer_1.2.1        tzdb_0.1.2           parallelly_1.28.1    ModelMetrics_1.2.2.2 R6_2.5.1            
 [91] lifecycle_1.0.0      polspline_1.1.19     datawizard_0.2.0.1   curl_4.3.2           GIGrvg_0.5          
 [96] minqa_1.2.4          limSolve_1.5.6       gdata_2.18.0         robustbase_0.93-8    TH.data_1.0-10      
[101] prediction_0.3.14    RColorBrewer_1.1-2   gower_0.2.2          htmlwidgets_1.5.4    rvest_1.0.1         
[106] rPref_1.3            globals_0.14.0       insight_0.14.4       htmlTable_2.2.1      codetools_0.2-18    
[111] matrixStats_0.60.1   randomForest_4.6-14  gtools_3.9.2         dbplyr_2.1.1         gtable_0.3.0        
[116] DBI_1.1.1            httr_1.4.2           stringi_1.7.4        diptest_0.76-0       timeDate_3043.102   
[121] mice_3.13.0          xml2_1.3.2           boot_1.3-28          ggeffects_1.1.1      heplots_1.3-8       
[126] DEoptimR_1.0-9       sjstats_0.18.1       jpeg_0.1-9           pkgconfig_2.0.3      knitr_1.34         